﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Functions Exercise 3</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function that finds all the occurrences of word in a text.</h1>
    <label for="word-input">Word:</label>
    <input type="text" id="word-input" />
    <label for="text-input">Text:</label>
    <input type="text" id="text-input" />
    <button onclick="execute()">Execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function findWords(word, text, caseSence)
        {
            caseSence = caseSence || false;
            if (false == caseSence)
            {
                word = word.toLowerCase();
                text = text.toLowerCase();
            }

            for (var textIdx = 0; textIdx < text.length; textIdx++)
            {
                var match = true;
                var localIdx = textIdx;

                for (var wordIdx = 0; wordIdx < word.length; wordIdx++)
                {
                    if (text[localIdx] != word[wordIdx])
                    {
                        match = false;
                        break;
                    }
                    localIdx++;
                }

                if (false != match)
                {
                    jsConsole.writeLine(word + " found at index " + textIdx);
                }
            }
        }

        function execute()
        {
            var myWord = jsConsole.read("#word-input");
            var myText = jsConsole.read("#text-input");

            jsConsole.writeLine("Find words with default case sensitive (false)");
            jsConsole.writeLine(findWords(myWord, myText));

            jsConsole.writeLine("Find words with case sensitive = true");
            jsConsole.writeLine(findWords(myWord, myText, true));

            jsConsole.writeLine("Find words with case sensitive = false");
            jsConsole.writeLine(findWords(myWord, myText, false));
        }

    </script>
</body>
</html>
